package app.com.myanr;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import static java.lang.Thread.sleep;

/**
 * Created by Administrator on 2017/9/12.
 */

public class HandleActivity extends Activity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        textView = (TextView) findViewById(R.id.text);


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.e("HandleActivity", "一秒后执行这个操作");
            }
        }, 1000);

        new MyThread().start();

    }

    int mum = 1;
    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int mss = msg.what;  //传过来的数据
            if (mss == 1) {
                mum++;
                Log.d("======run", "" + mum);
                textView.setText(Thread.currentThread().getName() + "i =  " + mum);
            }
        }
    };
    boolean isrun;
    public class MyThread extends Thread {
        @Override
        public void run() {
            while (!isrun) {
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Message message = new Message();
                message.what = 1;
                myHandler.sendMessage(message);
            }
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        isrun = true;
//        myHandler.removeCallbacks(new MyThread());
    }
}
